using System;
using System.Collections.Generic;
using System.Text;

using System.Data.SqlClient;
using System.Data;

namespace G2007.CodeGen.Library.DB
{
    public interface ICGDatabase
    {
        bool CreateDatabase(string initialConnectionString , string dbName, out string error);
        bool CanConnect(string connectionString, string dbName, out string error);
        bool ExecuteScript(string connectionString , string dbName , string script, out string error);

        string GetName();
    }




    public class SQLConnector : ICGDatabase
    {



        public bool CreateDatabase(string initialConnectionString , string dbName , out string error)
        {
            error = "";
            try
            {
                using (SqlConnection sqlCon = new SqlConnection(initialConnectionString))
                {
                    sqlCon.Open();
                    int count = 0;
                    using (SqlCommand sqlCom = new SqlCommand("select count([name]) from master..sysdatabases where [name]='" + dbName + "'" , sqlCon))
                    {
                        count = (int)sqlCom.ExecuteScalar();
                    }

                    if (count == 0)
                    {
                        // the database does not exist
                        using (SqlCommand sqlCom = new SqlCommand("create database " + dbName , sqlCon))
                        {
                            sqlCom.ExecuteScalar();
                        }
                    }
                    sqlCon.Close();
                }

            }
            catch (Exception ex)
            {
                error = ex.Message;
                return false;
            }
            return true;
        }




        public bool CanConnect(string connectionString , string dbName, out string error)
        {
            error = "";
            try
            {
                using (SqlConnection sqlCon = new SqlConnection(connectionString))
                {
                    sqlCon.Open();
                    sqlCon.Close();
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return false;
            }
            return true;
        }




        public bool ExecuteScript(string connectionString , string dbName , string script, out string error)
        {
            error = "";
            try
            {
                using (SqlConnection sqlCon = new SqlConnection(connectionString))
                {
                    sqlCon.Open();
                    using (SqlCommand sqlCom = new SqlCommand(script , sqlCon))
                    {
                        sqlCom.ExecuteScalar();
                    }

                    sqlCon.Close();
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return false;
            }
            return true;
        }



    

        public string GetName()
        {
            return "SQL Server CG Provider";
        }

    }
}
